import store from '@/store'

// 拍照
export const openCamera = ({ success = () => {} }) => {
  const options = {
    quality: 50,
    destinationType: Camera.DestinationType.DATA_URL
  }
  navigator.camera.getPicture(onSuccess, onFail, options)

  function onSuccess (imageData) {
    success(imageData)
  }

  function onFail (message) {
    alert('拍照失败')
  }
}

// 上传签名照片
export const uploadSignaturePhoto = ({ imageData, callback = () => {} }) => {
  const ft = new FileTransfer()
  const options = new FileUploadOptions()
  const serverUrl = 'http://192.168.1.110:55578/api/base64ToImg'
  // const serverUrl = 'http://www.ypt56.net:8088/api/uploadImg'
  options.params = {}
  const user = store.state.user
  Object.assign(options.params, {
    uid: user.uid,
    un: user.un,
    rtk: user.rtk
  })
  options.fileKey = 'Picture'
  options.fileName = '签名.jpeg' // 加.png，后台说的
  options.httpMethod = 'POST'
  options.params.Picture = imageData
  options.params.imgName = '签名' // 不加.png，后台说的
  ft.upload(imageData, encodeURI(serverUrl), res => {
    const response = JSON.parse(res.response)
    if (response.status === 200) {
      callback(response.data[0])
      alert('上传成功')
    } else {
      alert(response.msg)
    }
    // alert(JSON.stringify(res.response))
  }, error => {
    alert('上传失败')
  }, options)
}

// 上传箱门、封号照片 // 默认箱门0
export const uploadEirPhoto = ({ imageData, callback = () => {}, type = '', boxId = '' }) => {
  const ft = new FileTransfer()
  const options = new FileUploadOptions()
  const serverUrl = 'http://www.ypt56.net:8088/api/uploadCartonImg'
  // const serverUrl = 'http://192.168.1.110:55578/api/uploadCartonImg'
  options.params = {}
  const user = store.state.user
  if (!boxId.trim() || !type.trim()) {
    alert('参数错误' + boxId + ' ' + type)
    return
  }
  Object.assign(options.params, {
    uid: user.uid,
    un: user.un,
    rtk: user.rtk,
    boxId: boxId,
    type: type
  })
  if (type.trim()) {
    options.fileKey = 'Picture'
    options.fileName = '运单照片.jpeg' // 加.jpeg，后台说的
    options.httpMethod = 'POST'
    options.params.Picture = imageData
    options.params.imgName = '运单照片' // 不加.jpeg，后台说的
    ft.upload(imageData, encodeURI(serverUrl), res => {
      const response = JSON.parse(res.response)
      if (response.status === 200) {
        callback()
      } else {
        alert(response.msg)
      }
      // alert(JSON.stringify(res.response))
    }, error => {
      alert('上传失败')
    }, options)
  } else {
    alert('参数错误')
  }

}

